package com.aipanda.pojo;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import java.io.Serializable;

/**
 * @param property			属性
 * @param filterOperator	运算符	
 * @param value				值
 * @param ignoreCase		是否忽略大小写
 * @author wanyuxiang
 *
 */
public class SQLCondition implements Serializable {
	private static final long serialVersionUID = -8712382358441065075L;
	private String property;
	private Operator filterOperator;
	private Object value;
	private Boolean ignoreCase = false;

	public SQLCondition() {
	}
	
	/**
	 * @param eq		等于
	 * @param ne		不等于
	 * @param gt		大于
	 * @param lt		小于
	 * @param ge		大于等于
	 * @param le		小于等于
	 * @param like		相似
	 * @param in		包含
	 * @param isNull	为Null
	 * @param isNotNull	不为Null
	 * @author wanyuxiang
	 *
	 */
	public enum Operator {
		eq, ne, gt, lt, ge, le, like, in, isNull, isNotNull;

		public static Operator fromString(String value) {
			return valueOf(value.toLowerCase());
		}
	}

	public SQLCondition(String property, Operator operator, Object value) {
		this.property = property;
		this.filterOperator = operator;
		this.value = value;
	}

	public SQLCondition(String property, Operator operator, Object value,
			boolean ignoreCase) {
		this.property = property;
		this.filterOperator = operator;
		this.value = value;
		this.ignoreCase = Boolean.valueOf(ignoreCase);
	}

	public static SQLCondition eq(String property, Object value) {
		return new SQLCondition(property, Operator.eq, value);
	}

	public static SQLCondition eq(String property, Object value, boolean ignoreCase) {
		return new SQLCondition(property, Operator.eq, value, ignoreCase);
	}

	public static SQLCondition ne(String property, Object value) {
		return new SQLCondition(property, Operator.ne, value);
	}

	public static SQLCondition ne(String property, Object value, boolean ignoreCase) {
		return new SQLCondition(property, Operator.ne, value, ignoreCase);
	}

	public static SQLCondition gt(String property, Object value) {
		return new SQLCondition(property, Operator.gt, value);
	}

	public static SQLCondition lt(String property, Object value) {
		return new SQLCondition(property, Operator.lt, value);
	}

	public static SQLCondition ge(String property, Object value) {
		return new SQLCondition(property, Operator.ge, value);
	}

	public static SQLCondition le(String property, Object value) {
		return new SQLCondition(property, Operator.le, value);
	}

	public static SQLCondition like(String property, Object value) {
		return new SQLCondition(property, Operator.like, value);
	}

	public static SQLCondition in(String property, Object value) {
		return new SQLCondition(property, Operator.in, value);
	}

	public static SQLCondition isNull(String property) {
		return new SQLCondition(property, Operator.isNull, null);
	}

	public static SQLCondition isNotNull(String property) {
		return new SQLCondition(property, Operator.isNotNull, null);
	}

	public SQLCondition ignoreCase() {
		this.ignoreCase = Boolean.valueOf(true);
		return this;
	}

	public String getProperty() {
		return this.property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public Operator getOperator() {
		return this.filterOperator;
	}

	public void setOperator(Operator operator) {
		this.filterOperator = operator;
	}

	public Object getValue() {
		return this.value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public Boolean getIgnoreCase() {
		return this.ignoreCase;
	}

	public void setIgnoreCase(Boolean ignoreCase) {
		this.ignoreCase = ignoreCase;
	}

	public boolean equals(Object obj) {
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		if (this == obj){
			return true;
		}
		SQLCondition filter = (SQLCondition) obj;
		return new EqualsBuilder().append(getProperty(),
				filter.getProperty()).append(getOperator(),
				filter.getOperator()).append(getValue(),
				filter.getValue()).isEquals();
	}

	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(getProperty()).append(getOperator()).append(getValue()).toHashCode();
	}
}
